package com.tiansu.reborn.wanda.modules.executor.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tiansu.reborn.wanda.modules.executor.dao.TVPaymentDeviceDao;
import com.tiansu.reborn.wanda.modules.executor.entity.MakeupRechargeEntity;
import com.tiansu.reborn.wanda.modules.executor.entity.QueryRecharge;
import com.tiansu.reborn.wanda.modules.executor.entity.TvPaymentDevice;
import com.tiansu.reborn.wanda.modules.executor.utils.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class UploadDataListener extends AnalysisEventListener<MakeupRechargeEntity> {


    private TVPaymentDeviceDao tvPaymentDeviceDao;

    private String url;

    public UploadDataListener(TVPaymentDeviceDao tvPaymentDeviceDao,String url) {
        this.tvPaymentDeviceDao = tvPaymentDeviceDao;
        this.url=url;
    }

    @Override
    public void invoke(MakeupRechargeEntity data, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", JSONUtil.parse(data).toStringPretty());
        TvPaymentDevice tvPaymentDevice = tvPaymentDeviceDao.selectById(data.getDeviceId());
        if (null==tvPaymentDevice||tvPaymentDevice.getBrandid()<=0){
            log.info("未找到对应的设备信息");
            return;
        }

        QueryRecharge recharge=new QueryRecharge();
        recharge.setRechargeType(4);
        recharge.setBrandId(tvPaymentDevice.getBrandid());
        recharge.setSquareId(tvPaymentDevice.getPlazaid());
        recharge.setBerthNumber(tvPaymentDevice.getNickname());
        recharge.setOperater("admin");
        recharge.setEqualizeReason(data.getReason());
        recharge.setDeviceId(data.getDeviceId());
        recharge.setRechargeMoney(data.getMoney());

        Map<String, String> map = new HashMap<>();
        Gson g = new GsonBuilder().create();
        // 一次只处理一条
        map.put("Inputs",g.toJson(recharge));
        String result = OkHttpUtil.postFormParams(url, map);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
